/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-1-12 下午1:15 Created by dwzhang
 */
package com.jade.framework.base.util;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jade.framework.base.ErrorConstants;
import com.jade.framework.base.lang.StatusException;
/**
 * 响应工具类。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-1-12 下午1:15
 */
public class ResponseUtils
{
    public static final int STATUS_SUCCSS = 200;
    public static final int STATUS_ERROR = 500;
    public static final String ERR_UNEXPECTED = ErrorConstants.ERR_UNEXPECTED;
    public static final String PARAM_STATUS = "status";
    public static final String PARAM_ERROR_CODE = "errorCode";
    public static final String PARAM_ERROR_MESSAGE = "errorMessage";
    public static final String PARAM_ERROR_ARGS = "errorArgs";
    public static final String PARAM_DATA = "rows";
    public static final String PARAM_OFFSET = "offset";
    public static final String PARAM_TOTAL_COUNT = "totalCount";
    public static final String PARAM_PAGE_SIZE = "pageSize";
    public static final String PARAM_EXTEND_PARAMS = "params";
    public static final String CT_JSON = "application/json; charset=utf-8";

    protected static ObjectMapper getObjectMapper()
    {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        return objectMapper;
    }

    public static void writeSuccessResult(HttpServletResponse response)
            throws Exception
    {
        writeSuccessResult(response, null);
    }

    public static void writeSuccessResult(HttpServletResponse response, Object data)
            throws Exception
    {
        ObjectMapper objectMapper = getObjectMapper();
        Map resultMap = new HashMap();
        resultMap.put(PARAM_STATUS, STATUS_SUCCSS);
        if (data != null) {
            resultMap.put(PARAM_DATA, data);
        }
        response.setContentType(CT_JSON);
        objectMapper.writeValue(response.getWriter(), resultMap);
    }

    public static void writeSuccessResult(HttpServletResponse response, String key,Object data) throws Exception
    {
        ObjectMapper objectMapper = getObjectMapper();
        Map resultMap = new HashMap();
        resultMap.put(PARAM_STATUS, STATUS_SUCCSS);
        if (data != null) {
            resultMap.put(key, data);
        }
        response.setContentType(CT_JSON);
        objectMapper.writeValue(response.getWriter(), resultMap);
    }

    @Deprecated
    public static void writeSuccessPagingResult(HttpServletResponse response, int offset, int pageSize, Object data,
                                                int totalCount)
            throws Exception
    {
        ObjectMapper objectMapper = getObjectMapper();
        Map resultMap = new HashMap();
        resultMap.put(PARAM_STATUS, STATUS_SUCCSS);
        resultMap.put(PARAM_OFFSET, offset);
        resultMap.put(PARAM_PAGE_SIZE, pageSize);
        resultMap.put(PARAM_TOTAL_COUNT, totalCount);
        resultMap.put(PARAM_DATA, data);
        response.setContentType(CT_JSON);
        objectMapper.writeValue(response.getWriter(), resultMap);
    }

    @Deprecated
    public static void writeSuccessPagingResult(HttpServletResponse response, int offset, int pageSize, Object data,
                                                int totalCount, Map<String, Object> params)
            throws Exception
    {
        ObjectMapper objectMapper = getObjectMapper();
        Map resultMap = new HashMap();
        resultMap.put(PARAM_STATUS, STATUS_SUCCSS);
        resultMap.put(PARAM_OFFSET, offset);
        resultMap.put(PARAM_PAGE_SIZE, pageSize);
        resultMap.put(PARAM_TOTAL_COUNT, totalCount);
        resultMap.put(PARAM_DATA, data);
        resultMap.put(PARAM_EXTEND_PARAMS, params);
        response.setContentType(CT_JSON);
        objectMapper.writeValue(response.getWriter(), resultMap);
    }

    public static void writeSuccessPagingResult(HttpServletResponse response, PagingResult result)
            throws Exception
    {
        ObjectMapper objectMapper = getObjectMapper();
        response.setContentType(CT_JSON);
        objectMapper.writeValue(response.getWriter(), result);
    }

    public static void writeErrorResult(HttpServletResponse response, int status, String errorCode, String errorMessage,
                                        Object... errorArgs)
            throws Exception
    {
        ObjectMapper objectMapper = getObjectMapper();
        Map resultMap = new HashMap();
        resultMap.put(PARAM_STATUS, status);
        resultMap.put(PARAM_ERROR_CODE, errorCode);
        resultMap.put(PARAM_ERROR_MESSAGE, errorMessage);
        if (errorArgs != null) {
            resultMap.put(PARAM_ERROR_ARGS, errorArgs);
        }
        response.setContentType(CT_JSON);
        objectMapper.writeValue(response.getWriter(), resultMap);
    }

    public static void writeErrorResult(HttpServletResponse response, int status, StatusException e)
            throws Exception
    {
        writeErrorResult(response, status, e.getStatusCode(), e.getMessage(), e.getArguments());
    }

    public static void writeErrorResult(HttpServletResponse response, StatusException e)
            throws Exception
    {
        writeErrorResult(response, STATUS_ERROR, e);
    }

    public static void writeErrorResult(HttpServletResponse response, Throwable t)
            throws Exception
    {
        writeErrorResult(response, STATUS_ERROR, ERR_UNEXPECTED, t.getMessage());
    }
}
